﻿using System;
using System.Collections.Generic;
using System.Linq;
using MyDMS.DAL;

public partial class Users : PageBaseClass
{
    readonly MyDMSEntity _myDmsEntity=new MyDMSEntity();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<UserRole> roles = _myDmsEntity.UserRole.ToList();

            ddlUserRole.DataSource = roles;
            ddlUserRole.DataTextField = "Role_Name";
            ddlUserRole.DataValueField = "Role_Code";
            ddlUserRole.DataBind();


            List<Department> deptList =
                _myDmsEntity.Department.ToList();

            ddlDept.DataSource = deptList;
            ddlDept.DataTextField = "Department_Name";
            ddlDept.DataValueField = "Department_Code";
            ddlDept.DataBind();

            if (deptList.Count > 0)
            {
                ddlDept.SelectedIndex = 0;
                BindTheUsers(int.Parse(ddlDept.SelectedValue));
            }
            else
                BindTheUsers(0);

        }
    }

    private void BindTheUsers(int deptCode)
    {
        if (deptCode > 0)
        {
            var users = from u in _myDmsEntity.User.Where(ui => ui.Department_Code == deptCode)
                    select new
                               {
                                   u.User_Name,
                                   u.User_Password,
                                   u.FirstName,
                                   u.LastName,
                                   u.Email_Address,
                                   u.Street_Address,
                                   u.City,
                                   u.Sex,
                                   DepartmentName=_myDmsEntity.Department.FirstOrDefault(d=>d.Department_Code==u.Department_Code).Department_Name
                               };
            this.gvUsers.DataSource = users;
            this.gvUsers.DataBind();
        }
        else
        {
            var users = from u in _myDmsEntity.User
                    select new
                               {
                                   u.User_Name,
                                   u.User_Password,
                                   u.FirstName,
                                   u.LastName,
                                   u.Email_Address,
                                   u.Street_Address,
                                   u.City,
                                   u.Sex,
                                   DepartmentName=_myDmsEntity.Department.FirstOrDefault(d=>d.Department_Code==u.Department_Code).Department_Name
                               };
            this.gvUsers.DataSource = users;
            this.gvUsers.DataBind();
        }
    }

    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindTheUsers(int.Parse(ddlDept.SelectedValue));
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        User user = new User();
        user.User_Name = txtUserName.Text;
        user.User_Password = txtPassword.Text;
        user.FirstName = txtFirstName.Text;
        user.LastName = txtLastName.Text;
        user.Email_Address = txtEmail.Text;
        user.Sex = radMale.Checked ? radMale.Text : radFemale.Text;
        user.Street_Address = txtAddress.Text;
        user.City = txtCity.Text;

        user.Department_Code = int.Parse(ddlDept.SelectedValue);

        user.Online_Status = "Offline";

        user.Is_Approved = true;
        user.Approved_By = Author.user_Code;
        user.Created_By = Author.user_Code;

        user.Creation_Date = DateTime.Now;
        user.Updation_Date = DateTime.Now;

        _myDmsEntity.User.AddObject(user);

        if (_myDmsEntity.SaveChanges()>0)
        {
            UserInRole uir = new UserInRole();
            uir.User_Code = user.User_Code;
            uir.Role_Code = int.Parse(ddlUserRole.SelectedValue);
            uir.Creation_Date = DateTime.Now;
            uir.Updation_Date = DateTime.Now;

            _myDmsEntity.UserInRole.AddObject(uir);
            _myDmsEntity.SaveChanges();

            lblMessage.Text = "Saved Successfully";
        }
        else
        {
            lblMessage.Text = "Error!";
        }
        lblMessage.Visible = true;
    }
}
